package com.crm.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.PowerMapper;
import com.crm.model.mpojo.Power;
import com.crm.model.mpojo.Role;
import com.crm.model.mpojo.Users;
import com.crm.services.PowerService;
import com.crm.util.MapmenusUtil;
import com.github.pagehelper.PageInfo;

@Controller
public class PowerController {
	@Autowired
	PowerService powerService;

	@Autowired
	PowerMapper powerMapper;

	/**
	 * 菜单列表
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/authority/menus")
	public ModelAndView role(Integer pageNum, Integer pageSize,Integer statusid  ,String search)  {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("/authority/menus");
		List<Map<String, String>> selectAllTable = powerService.selectByPager(pageNum, pageSize,statusid,search);
		String jsonString = JSONObject.toJSONString(selectAllTable);
		// 获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAllTable);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);
		return mv;
	}
	
	/**
	 * 分页查询菜单
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/authority/menus")
	@ResponseBody	
	public PageInfo<Map<String, String>> user(Integer pageNum, Integer pageSize,Integer statusid  ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = powerService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}

	/**
	 * 添加菜单页面数据
	 * 
	 * @return
	 */
	@RequestMapping("/authority/addMenusview")
	@ResponseBody
	public JSONObject addMenusview() {
		JSONObject jsonarray = new JSONObject();
		List<Power> list = powerMapper.selectParentId(0);
		
		jsonarray.put("parent", list);
		return jsonarray;
	}

	/**
	 * 修改菜单页数据
	 * 
	 * @return
	 */
	@PostMapping("/authority/updateMenusview")
	@ResponseBody
	public JSONObject updateMenusview(@RequestParam(name = "ID", required = false) Integer pId) {
		JSONObject jsonarray = new JSONObject();
		Power power = powerMapper.selectByPrimaryKey(pId);
		// 获取顶级菜单
		List<Power> list = powerMapper.selectParentId(0);
		jsonarray.put("menurs", power);
		jsonarray.put("parent", list);
		return jsonarray;
	}

	/**
	 * 添加菜单
	 * 
	 * @return
	 */
	@RequestMapping("/authority/addMenus")
	@ResponseBody
	public JSONObject addMenus(@RequestParam(name = "pName", required = false) String pName,
			@RequestParam(name = "pUrl", required = false) String pUrl,
			@RequestParam(name = "pSeq", required = false) Integer pSeq,
			@RequestParam(name = "pIcon", required = false) String pIcon,
			@RequestParam(name = "pDescript", required = false) String pDescript,
			@RequestParam(name = "parentId", required = false) Integer parentId,
			@RequestParam(name = "pState", required = false) Integer pState) {
		JSONObject jsonarray = new JSONObject();
		Power power = new Power();
		power.setParentId(parentId);
		power.setpName(pName);
		power.setpSeq(pSeq);
		power.setpUrl(pUrl);
		power.setpIcon(pIcon);
		power.setpDescript(pDescript);
		power.setpState(pState);
		power.setpType(1);
		powerService.save(power);
		return jsonarray;
	}

	/**
	 * 修改菜单列表
	 * 
	 * @return
	 */
	@RequestMapping("/authority/updateMenus")
	@ResponseBody
	public JSONObject updateMenus(@RequestParam(name = "pId", required = false) Integer pId,
			@RequestParam(name = "pName", required = false) String pName,
			@RequestParam(name = "pUrl", required = false) String pUrl,
			@RequestParam(name = "pSeq", required = false) Integer pSeq,
			@RequestParam(name = "pIcon", required = false) String pIcon,
			@RequestParam(name = "pDescript", required = false) String pDescript,
			@RequestParam(name = "parentId", required = false) Integer parentId,
			@RequestParam(name = "pState", required = false) Integer pState) {
		JSONObject jsonarray = new JSONObject();
		Power power = new Power();
		power.setpId(pId);
		power.setParentId(parentId);
		power.setpName(pName);
		power.setpSeq(pSeq);
		power.setpUrl(pUrl);
		power.setpIcon(pIcon);
		power.setpDescript(pDescript);
		power.setpState(pState);
		powerMapper.updateByPrimaryKeySelective(power);
		return jsonarray;
	}

	@PostMapping("/authority/singlefile")
	@ResponseBody
	public String addImage(MultipartFile file) {
		System.out.println(file);
		
		// 文件上传
		if (!file.isEmpty()) {
			try {
				// 图片命名
				String newCompanyImageName = "newPIC";
				String newCompanyImagepath = "D:\\" + newCompanyImageName;
				File newFile = new File(newCompanyImagepath);
				if (!newFile.exists()) {
					newFile.createNewFile();
				}
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
				out.write(file.getBytes());
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "图片上传失败！";
			} catch (IOException e) {
				e.printStackTrace();
				return "图片上传失败！";
			}
		}
		return "图片上传失败！";

	}

}
